import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_wgtool/OSNet/Core/OSNetError.dart';
import 'package:flutter_wgtool/OSNet/Dao/WGConfigDao.dart';
import 'package:flutter_wgtool/Widget/WGBanner.dart';

class WGHomePage extends StatefulWidget {
  const WGHomePage({Key? key}) : super(key: key);

  @override
  _WGHomePageState createState() => _WGHomePageState();
}

class _WGHomePageState extends State<WGHomePage> {
  var imgesArr;

  List<Widget> childrens = [
    Padding(padding: EdgeInsets.fromLTRB(0, 50, 0, 0)),
    Text(
      "WGTool",
      style: TextStyle(
          color: Colors.black,
          decoration: TextDecoration.none,
          fontSize: 50,
          fontWeight: FontWeight.w800),
    ),
    Padding(padding: EdgeInsets.fromLTRB(0, 20, 0, 0)),
    Text(
      "源码更新：20210712",
      style: TextStyle(
          color: Colors.green, decoration: TextDecoration.none, fontSize: 18),
    ),
  ];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    /// FutureBuilder
    childrens.add(SizedBox(
      height: 50,
    ));
    childrens.add(_addFutureBuild());
    // childrens.add(_buildTips());
  }

  @override
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;
    final width;
    final height;
    if (size.width > size.height * 0.8) {
      /// 横屏 OR PC浏览器
      width = size.height * 0.6;
      height = size.height;
    } else {
      /// 移动端设备
      width = size.width;
      height = size.height;
    }
    return Container(
      child: Center(
        child: Container(
            color: Colors.orange,
            width: width,
            height: height,
            alignment: AlignmentDirectional.center,
            child: Column(children: childrens)),
      ),
    );
  }

  Future<List> getConfs() async {
    var list;
    try {
      var result = await WGConfigDao.configs();
      var res = jsonDecode(result);
      if (res["code"] == 200) {
        ///获取到配置
        list = res["confs"];
        return list;
      }
    } on OSNetError catch (e) {
      print(e.message);
    }
    return list;
  }

  Widget _buildTips() {
    return Container(
      height: 80,
      alignment: Alignment.bottomCenter,
      child: Text(
        "择日更新，大家别催哈",
        style: TextStyle(
            fontSize: 18, color: Colors.green, decoration: TextDecoration.none),
      ),
    );
  }

  Widget _addFutureBuild() {
    return FutureBuilder(
        future: getConfs(),
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          // 请求已结束
          if (snapshot.connectionState == ConnectionState.done) {
            if (snapshot.hasError) {
              // 请求失败，显示错误
              return Text("Error: ${snapshot.error}");
            } else {
              // 请求成功，显示数据
              imgesArr = snapshot.data;
              // childrens.add(WGBanner(imgesArr));
              // childrens.add(_buildTips());
              return Column(
                children: [WGBanner(imgesArr), _buildTips()],
              );
            }
          } else {
            // 请求未结束，显示loading
            return Container(
                height: 300, child: Center(child: CircularProgressIndicator()));
          }
        });
  }
}
